document.onkeydown = function(e){
    if(!e) e = window.event;
    if((e.keyCode || e.which) == 13){
        document.getElementById("login_link").click();
    }
}
var clickflag=0;
function checkLogin(){
    if(clickflag==1){
        return false;
    }else{
        clickflag=1;
        var username = $('#username').val();
        var password = $('#password').val();
        if(isZFC(username)){
            $(".errmsg").css("visibility","visible");
            $(".usepwd").removeClass("erro");
            $(".usename").addClass("erro");
            $(".errmsg").text("用户名不能为空");
            clickflag=0;
            return false;
        }
        if(isZFC(password)){
            $(".errmsg").css("visibility","visible");
            $(".usename").removeClass("erro");
            $(".usepwd").addClass("erro");
            $(".errmsg").text("密码不能为空");
            clickflag=0;
            return false;
        }
        $('#login_link').text('登陆中...');
        $.ajax({
            type:'post',
            url:'/getLoginCheck/register.html',
            dataType:'json',
            data:{username:username},
            success:function(data){
                if(data.msg && data.msg == "success"){
                    var userid = data.userid;
                    var logincheck = data.logincheck;
                    if(data.logincheck == null || data.logincheck == "0"){
                        login(username,password);
                    }else if(data.logincheck == "1"){
                        $("#phone").val(data.phone);
                        var phoneview = data.phone.length > 7 ? data.phone.substring(0,3)+"****"+data.phone.substring(7) : data.phone;
                        $("#phoneview").val(phoneview);
                        $("#userid").val(data.userid);
                        phoneValidation();
                    }else if(data.logincheck == "2"){
                        pwdValidation();
                    }
                }else if(data.msg){
                    clickflag=0;
                    $('#login_link').text('登 陆');
                    $(".errmsg").css("visibility","visible");
                    $(".usepwd").removeClass("erro");
                    $(".usename").addClass("erro");
                    $(".errmsg").text(data.msg);
                }else{
                    clickflag=0;
                    $('#login_link').text('登 陆');
                    $(".errmsg").css("visibility","visible");
                    $(".usepwd").removeClass("erro");
                    $(".usename").addClass("erro");
                    $(".errmsg").text("未知错误");
                }
            },
            error:function(data) {
            }
        });
    }
}
function login2(){
    var username = $('#username').val();
    var password = $('#password').val();
    var checkvalue = $('#checkvalue').val();
    login(username,password,checkvalue);
}
function login(username,password,checkvalue){
    $.ajax({
        type:'post',
        url:'/login.action',
        dataType:'text',
        data:{username:username,password:password,checkvalue:checkvalue},
        success:function(data){
            clickflag=0;
            if(data=='success' || data=='sameusername'){
                $('#login_link').text('登陆成功');
                savelogininfo(username,password,document.getElementById('checkpass').checked);
                if(location.search && location.search.indexOf("backUrl") != -1){
                    window.location.href = location.search.substring(location.search.indexOf("backUrl")+8);
                }else{
                    window.location.href='/';
                }
                if(isLB()){
                    window.external.SetIESize ('1200','680');
                }
            }else{
                $('#login_link').text('登 陆');
                $(".errmsg").css("visibility","visible");
                $(".usepwd").removeClass("erro");
                $(".usename").addClass("erro");
                $(".errmsg").text(data);
            }
        },
        error:function(data) {
            //showmessage("出现异常！");
            savelogininfo(username,password,document.getElementById('checkpass').checked);
            window.location.href='/';
            if(isLB()){
                window.external.SetIESize ('1200','680');
            }
        }
    });
}
function isZFC(t){
    if(t==null){
        return true;
    }
    if(t.replace(/(^\s*)|(\s*$)/g,'')==''){
        return true;
    }
    return false;
}
$(document).ready(function(e) {
    //读取cookie
    readlogininfo('username','namelist','password','checkpass');
    var thi = $("#username").get(0);
    var thi2 = $("#password").get(0);
    wriRes(thi.value,thi);
    wriRes(thi2.value,thi2);

    $(".tel").focus(function(){
        $(this).removeClass("arrow-down").addClass("arrow-up").siblings(".tel-list").show();
    });
    $(".tel-list dd").click(function(){
        $(this).parent().hide();
        $(".tel").val($(this).text());
        getlogininfopass($(this).text(),'password','checkpass');
        wriRes(thi2.value,thi2);
    });
    $(".tel").blur(function(){
        $(this).removeClass("arrow-up").addClass("arrow-down");
    });
    $("#password").focus(function(){
        $(".tel-list").hide();
        $(this).removeClass("arrow-up").addClass("arrow-down");
    });

    // //层内的link点击事件，注意让事件停止冒泡  失去焦点处理
    $(".tel").click(function(event){
        event=event||window.event;
        event.stopPropagation();
    });
    //点击层外，隐藏层
    $(document).click(function(e){
        $(".tel-list").hide();
    });
});
function sendCode(){
    var phone = $("#phone").val();
    var type = $("#type").val();
    var userid = $("#userid").val();
    var sendCodeTime = $("#sendCodeTime").val();
    $.ajax("/registerSendCode/register.html",
        {
            type : "POST",
            async : true,
            data : {
                "phone" : phone,
                "type" : type,
                "userid" : userid,
                "sendCodeTime":sendCodeTime
            },
            success : function(jsonall) {
                if (jsonall && jsonall.msg) {
                    if (jsonall.msg != "success") {
                        $(".errmsg").css("visibility","visible");
                        $(".yzmtxt").addClass("erro");
                        $(".errmsg").text(jsonall.msg);
                    }else{
                        $("#send").attr("onclick","");
                        djs(60);
                    }
                }
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert("请求出现异常，错误代码：" + textStatus + ";"
                    + XMLHttpRequest.status);
            }
        });
}
function djs(s){
    if(s==0){
        $("#send").attr("onclick","sendCode()");
        var type = $("#type").val();
        if(type == "0"){
            $("#send").html("发送短信验证码");

        }else if(type == "1"){
            $("#send").html("发送语音验证码");
        }
    }else{
        $("#send").html(s+"秒后可再发送");
        setTimeout(function(){djs(--s);},1050);
    }
}
function codeType(){
    var type = $("#type").val();
    if(type == "0"){
        $("#type").val(1);
        $("#send").html("发送语音验证码");
        $("#qh").html("短信验证");
    }else if(type == "1"){
        $("#type").val(0);
        $("#send").html("发送短信验证码");
        $("#qh").html("语音验证");
    }
}
function popupbox(popid){
    var pop = "#"+popid;
    var w = $(pop).outerWidth();
    var h = $(pop).outerHeight();
    var _dl = document.documentElement.scrollTop||document.body.scrollTop;
    var l = Math.round((document.documentElement.clientWidth - w) / 2 + document.documentElement.scrollLeft);
    var t = Math.round((document.documentElement.clientHeight - h) / 2 + _dl);
    $(pop).css({"top":t,"left":l});
    $(pop).find(".pop-box-bg").css({"width":w,"height":h});
    var ch = document.documentElement.scrollHeight;
    var cw = document.documentElement.scrollLeft+document.documentElement.clientWidth;
    var cover = document.createElement("div");
    cover.id = "cover";
    cover.style.position = "absolute";
    cover.style.top = "0px";
    cover.style.left = "0px";
    cover.style.width = cw+"px";
    cover.style.height = ch + "px";
    cover.style.zIndex = "999";
    cover.style.filter = "alpha(opacity=0)";
    cover.style.opacity = "0";
    cover.style.display = "block";
    cover.style.background = "#252130";
    cover.innerHTML = '<iframe id="if" name="if" style="position:absolute;top:-5px;left:0;border:none;width:100%;height:100%;filter:alpha(opacity=0);" ></iframe>';
    document.body.appendChild(cover);
    $("#cover").animate({opacity:0.8}, 100, function() {
        $(pop).fadeIn(300);
    });
    $(window).resize(function(){
        var ncw = document.documentElement.scrollLeft+document.documentElement.clientWidth;
        $("#cover").width(ncw);
    });
};
function closebox(){
    layer.closeAll();
}

function pwdValidation(){
    layer.open({
        type: 1,
        title: false,
        area: "312px",
        shadeClose: false,
        skin: "yzhide",
        content: $("#pwd-validation")
    });
}
function phoneValidation(){
    layer.open({
        type: 1,
        title: false,
        area: "312px",
        shadeClose: false,
        skin: "yzhide",
        content: $("#phone-validation")
    });
}